<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : productclass.class.php   FILE_PATH : \\classes\application\productclass.class.php
 * ....关于商品类别的管理
 *
 * @copyright workflow_system
 * @author workflow Develop Team 
 * @package
 * @subpackage
 * @version Tue Aug 07 15:39:16 CST 2007
 */
class ProductCategoryClass extends FrameWork {

	/**
     * 获得商品类的数据库数组资料
     *
     * @return array
     */
	function getProductClass($condition){
		$conditionstr = $this->_conditionSearch($condition);
		$ProductClassArray = $GLOBALS['db']->GetList($conditionstr, $page, "product_class");
		return $ProductClassArray;
	}

	/**
	 * 构造检索条件
	 * @author     
	 * @param  
	 * @return 
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		/*父ID*/
		if ($condition['parent_id'] != '') {
			$condition_str .= " and pc_u_id = '". $condition['parent_id'] ."'";
		}
		/*排序*/
		if ($condition['order'] == '1') {
			$condition_str .= " order by pc_u_id asc,pc_sort asc,pc_id asc ";
		}else if ($condition['order'] == '2') {
			$condition_str .= " order by pc_deeppath asc,pc_u_id asc,pc_sort asc,pc_id asc ";
		}

		return $condition_str;
	}
	/**
	 * 获得商品类格式化后一维数组
	 * 
	 * 下标信息
	 * 多维 
	 * id,parentId,name,sort,depth,child(array)
	 * 一维
	 * id,parentId,name,sort,depth,haveson
	 * 
	 * @param mix $conditionstr
	 * @param object $Objpage
	 * @return array
	 */
	function listClassDetail($string="&nbsp;&nbsp;&nbsp;&nbsp;"){

		if (!is_file(BasePath."/cache/ProductClass_show_single.php")  || strlen(trim(file_get_contents(BasePath."/cache/ProductClass_show_single.php")))<25 ){
			$result = $this->restartClass();
			if($result !== true){
				return $result;
			}
		}
		include BasePath."/cache/ProductClass_show_single.php";
		if (is_array($node_cache)){
			foreach ($node_cache as $k => $v){
				$line = str_repeat($string,$v[4]);
				$node_cache[$k][2] = $line.$v[2];
				/*增加页面输出的下拉菜单下标参数*/
				$node_cache[$k]['id'] = $node_cache[$k][0];
				$node_cache[$k]['name'] = $node_cache[$k][2];
			}
		}
		return  $node_cache;
	}

	/**
	 * 获得商品分类详细信息
	 *
	 * @param int $pc_id
	 * @return array $pclass_row
	 */
	function getPClassRow($pc_id){
		$pclass_row = $GLOBALS['db']->GetList("and pc_id='" . $pc_id . "'",$page,'product_class');
		return $pclass_row[0];
	}
	/**
	 * 获得指定ID的商品分类信息
	 *
	 * @param unknown_type $pc_id
	 * @return unknown
	 */
	function getPcateRow($pc_id){
		$array = $this->listClassDetail();
		if (is_array($array)) {
			foreach ($array as $v){
				if ($v['id'] == $pc_id){
					return $v;
				}
			}
		}
		return true;
	}
	/**
	 * 添加商品分类
	 *
	 * @param array $input
	 * @return bool
	 */
	function addProductClass($input){
		$valueArray=array();
		$valueArray['pc_name'] = $input['txtPCName'];
		$valueArray['pc_u_id'] = $input['slPCUId'];
		$valueArray['pc_intro']= $input['txtPCIntro'];
		$valueArray['pc_sort'] = $input['txtSort'];
		$valueArray['pc_keywords'] = $input['txtKeywords'];
		if (!empty($input['brand_id'])) {
			$valueArray['pc_brand_id'] = serialize($input['brand_id']);
		}
		$parent_array = $GLOBALS['db']->GetOneRow($input['slPCUId'],"product_class","pc_id","pc_deeppath");
		$valueArray['pc_deeppath'] = intval($parent_array['pc_deeppath'])+1;
		if ($GLOBALS['db']->InsertRow($valueArray, 'product_class', 'pc_id')){
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 更新商品分类
	 *
	 * @param array $input
	 * @return bool
	 */
	function updateProductClass($input){
		$valueArray = array();
		$valueArray['pc_id'] = $input['txtPCId'];
		if ($input['txtPCName'] != ''){
			$valueArray['pc_name'] = $input['txtPCName'];
		}
		if ($input['slPCUId'] != ''){
			$valueArray['pc_u_id'] = $input['slPCUId'];
		}
		if ($input['txtPCState'] != ''){
			$valueArray['pc_state']= $input['txtPCState'];
		}
		if ($input['txtPCIntro'] != ''){
			$valueArray['pc_intro']= $input['txtPCIntro'];
		}
		if ($input['txtSort'] != ''){
			$valueArray['pc_sort'] = $input['txtSort'];
		}
		if ($input['txtHighlight'] != ""){
			$valueArray['is_highlight'] = $input['txtHighlight'];
		}
		if ($input['txtKeywords'] != ""){
			$valueArray['pc_keywords'] = $input['txtKeywords'];
		}
		$valueArray['pc_brand_id'] = serialize($input['brand_id']);
		
		$parent_array = $GLOBALS['db']->GetOneRow($input['slPCUId'],"product_class","pc_id","pc_deeppath");
		$valueArray['pc_deeppath'] = intval($parent_array['pc_deeppath'])+1;
		$insert_rs = $GLOBALS['db']->UpdateRow($valueArray['pc_id'],$valueArray, 'product_class', 'pc_id');
		if ($insert_rs){
			return true;
		}else{
			return false;
		}
		
	}
	/**
	 * 删除商品分类
	 *
	 * @param array $input 商品分类ID
	 * @return Boolean
	 */
	function delProductClass($input){
		if ($input != ''){
			if (is_array($input)){
				foreach ($input as $k => $v){
					$GLOBALS['db']->DelRow($v,'product_class','pc_id');
				}
			}else {
				$GLOBALS['db']->DelRow($input,'product_class','pc_id');
			}
			$this->restartClass();/*重新生成数组文件*/
			return true;
		}else {
			return false;
		}
	}

	/**
	 * 重新生成商品分类
	 * 两种形式 一维的和多维的
	 */
	function restartClass(){
		require_once("fileoperate.class.php");
		/**
		 * 生成 多维数组 形式的
		 */
		$condition['order'] = '2';
		$ar = $this->getProductClass($condition);
		//定义目标数组
		$d = array();
		//定义索引数组，用于记录节点在目标数组的位置
		$ind = array();

		foreach($ar as $v) {
			$v['child'] = array(); //给每个节点附加一个child项
			if($v['pc_u_id'] == 0) {
				$i = count($d);
				$d[$i] = $v;
				$ind[$v['pc_id']] =& $d[$i];
			}else {
				$i = count($ind[$v['pc_u_id']]['child']);
				$ind[$v['pc_u_id']]['child'][$i] = $v;
				$ind[$v['pc_id']] =& $ind[$v['pc_u_id']]['child'][$i];
			}
		}
		
		/*将生成的多维数组写入文件，用于前台*/
		if (is_array($d)){
			$string = $this->makeArray($d,array(0=>'pc_id',1=>'pc_u_id',2=>'pc_name',3=>'pc_sort','4'=>'is_highlight'),0);
		}
		$creatfile = "../cache/ProductClass_show.php";

		$make_p_class_mul = FileOperate::makeProductClassFlie($string,$creatfile);
		if ($make_p_class_mul !== true) {
			return $make_p_class_mul;
		}
		
		/*生成一维数组，用于后台新增和修改页面的下拉列表*/

		$result = array();
		$string = $this->getOneLevelArray($result,$d,'0',array(0=>'pc_id',1=>'pc_u_id',2=>'pc_name',3=>'pc_sort',4=>'is_highlight'));
		$file_string = $this->getOneLevelString($string);
		$creatfile = "../cache/ProductClass_show_single.php";
		$make_p_class_single = FileOperate::makeProductClassFlie($file_string,$creatfile);
		if ($make_p_class_single !== true) {
			return $make_p_class_single;
		}
		return true;
	}
	
	/**
	 * 递归取商品类别
	 * @author workflow Develop Team     
	 * @param  
	 * @return array
	 */
	function getProductClassArray($pc_u_id='0'){
		$condition['parent_id'] = $pc_u_id;
		$condition['order'] = '1';
		$class_list = $this->getProductClass($condition);
		unset($condition);
		if (!empty($class_list)){
			foreach ($class_list as $k => $v){
				$class_list[$k]['child'] = $this->getProductClassArray($v['pc_id']);
			}
		}
		return $class_list;
	}
	


	/**
	 * 构成商品类目的多维数组
	 * 
	 * @author     
	 * @param  
	 * @return int/bool/object/array
	 */
	function makeArray($d,$Fields,$depth=0){
		if (is_array($d)){
			/*
			foreach ($d as $k => $v){
			$file_string .="\n       array(".$v[$Fields[0]].",";
			$file_string .="\n        ".$v[$Fields[1]].",";
			$file_string .="\n        '".$v[$Fields[2]]."',";
			$file_string .="\n        ".$v[$Fields[3]].",";
			$file_string .="\n        ".$depth.",";
			$file_string .="\n        ".$v[$Fields[4]].",";
			if (!empty($v['child'])) {
			$line = $depth+1;
			$file_string .="\n        array(".$this->makeArray($v['child'],$Fields,$line)."),";
			}
			$file_string .="\n       ), \n";
			}
			}
			*/
			foreach ($d as $k => $v){
				$file_string .="array('".$v[$Fields[0]]."','".$v[$Fields[1]]."','".Common::magic_value($v[$Fields[2]])."','".$v[$Fields[3]]."','".$depth."','".$v[$Fields[4]]."'";
				if (!empty($v['child'])) {
					$line = $depth+1;
					$file_string .=",array(".$this->makeArray($v['child'],$Fields,$line).")";
				}
				$file_string .="),\n";
			}
		}
		return $file_string;
	}

	/**
	 * 生成商品类目的一维数组
	 * @author     
	 * @param  
	 * @return array
	 */
	function getOneLevelArray(&$result, $node, $depth='0',$Fields){
		if (is_array($node)) {
			foreach ($node as $k => $v){
				$num = count($result);
				$result[$num][0] = $v[$Fields[0]];
				$result[$num][1] = Common::magic_value($v[$Fields[1]]);
				$result[$num][2] = Common::magic_value($v[$Fields[2]]);
				$result[$num][3] = $v[$Fields[3]];
				$result[$num][4] = $depth;
				if (is_array($v['child'])){
					$line = $depth+1;
					$result = $this->getOneLevelArray($result, $v['child'], $line,$Fields);
				}
				unset($num);
			}
		}
		return $result;
	}

	/**
	 * 取指定ID的所有下级类别 一维数组
	 * $class_array 类别数组,$result结果集,$pc_id父类ID
	 * @author     
	 * @param  
	 * @return array
	 */

	function getArrayById($class_array,$result,$pc_id){

		if (is_array($class_array)){
			foreach ($class_array as $v){
				if ($v[1] == $pc_id){
					$num = count($result);
					$result[$num] = $v;
					unset($num);
					if ($v[5] == 1) {
						$result = $this->getArrayById($class_array,$result,$v[0]);
					}
				}
			}
		}
		return $result;
	}

	/**
	 * 把一维数组拼成字符串
	 * 
	 * @author     
	 * @param  
	 * @return string
	 */
	function getOneLevelString($param){
		if (is_array($param)) {
			foreach ($param as $k => $v){
				$file_string .="array('".$v[0]."','".$v[1]."','".$v[2]."','".$v[3]."','".$v[4]."',";
				/*
				$file_string .="\n       array(".$v[0].",";
				$file_string .="\n        ".$v[1].",";
				$file_string .="\n        '".$v[2]."',";
				$file_string .="\n        ".$v[3].",";
				$file_string .="\n        ".$v[4].",";
				*/
				foreach ($param as $k2 => $v2){
					$i = 0;
					if ($v[0] == $v2[1]){         /*$v['id'] == $v2['parentId']*/
						$i = 1;
						break;
					}
				}
				//haveson
				if ($i == 1){
					$file_string .="1,";
				}else {
					$file_string .="0,";
				}
				$file_string .="),";
			}
		}
		return $file_string;
	}

	/**
	 * 取指定类别ID的深度数
	 * @author workflow Develop Team     
	 * @param  $id
	 * @return int
	 */
	function getClassDeep($id){
		$class_array = $this->listClassDetail();
		if (is_array($class_array)){
			for ($i=0;$i<count($class_array);$i++){
				if ($class_array[$i][0] == $id){
					return $class_array[$i][4];
				}
			}
		}else {
			return '0';//第一级
		}
	}

	/************下面的需要整理***************/

	/**
	 * 将一维数组按分类级别转维多维数组
	 *
	 * @param var[] $arr
	 * @param int $parent_id
	 * @param var[] $newarr
	 * @return var[]
	 */
	function getAnyLevelClass($arr,$parent_id=0,&$newarr){

		if (is_array($arr)){
			$i = 0;
			foreach ($arr as $k=>$v){
				if ($parent_id == $v['parentId']){

					if ($parent_id > 0){

						$newarr[count($newarr)-1]['baby'][$i] = $v;
					}else{
						$newarr[count($newarr)] = $v;
					}

					$this->getAnyLevelClass($arr,$v['id'],$newarr);

					$i++;
				}
			}
		}
		return $newarr;
	}

	/**
	 * 得到某一级商品分类
	 *
	 * @param int $parent_id
	 * @return var[]
	 */
	function getOneLevelClass($arr,$parent_id){
		$newarr = array();
		if (is_array($arr)){
			foreach ($arr as $k=>$v){
				if ($parent_id == $v['parentId']){
					$newarr[count($newarr)] = $v;
				}
			}
		}
		return $newarr;
	}

	/**
	 * 取某父类下的分类
	 * @author workflow Develop Team
	 * @param  $arr 类别数组 $parentid父ID
	 * @return array
	 */
	function getClassArray($arr,$parentid){
		$newarr = array();
		if (is_array($arr)){
			foreach ($arr as $k => $v){
				if ($v['pc_u_id'] == $parentid){
					$newarr[] = $v;
				}
			}
		}
		return $newarr;
	}


	/**
	 * 取得商品分类树枝
	 *
	 * @param array $array
	 * @param int $node
	 * @return array
	 */
	function get_path($array, $node)
	{
		if(is_array($array)){
			foreach ($array as $key => $value){
				if($value[id] == $node){
					$path_array = $array[$key];
					break;
				}
			}
		}
		$path = array();
		if ($path_array['1']!="")
		{
			$path[] = $path_array;
			if($path_array['1']!="0"){
				$path = array_merge($this->get_path($array, $path_array['1']), $path);
			}

		}else{
			$path[] = $path_array;
		}
		return $path;
	}

	/**
	 * 判断新类别是否在删除类别下
	 * @author workflow Develop Team
	 * @param  $del_id 删除的类别ID，$new_id 转移到的类别ID
	 * @return int/bool/object/array
	 */
	function checkNewId($del_id,$new_id){
		if (!is_file(BasePath."/cache/ProductClass_show_single.php")  || strlen(trim(file_get_contents(BasePath."/cache/ProductClass_show_single.php")))<25 ){
			$this->restartClass();
		}
		include BasePath."/cache/ProductClass_show_single.php";

		if (is_array($node_cache)){
			foreach ($node_cache as $k => $v){
				if (($v['parentId'] == $del_id && $v['id'] == $new_id) ||  ($v['id'] == $del_id && $v['id'] == $new_id)){
					return "error";
				}
			}
		}
		$result = $this->finishingClass($node_cache,$del_id,$new_id);
		return $result;
	}

	function finishingClass($node,$del_id,$new_id){
		if (is_array($node)){
			foreach ($node as $k => $v){
				if ($v['parentId'] == $new_id){
					if ($v['id'] == $del_id){
						return "error";
					}else {
						$this->finishingClass($node,$del_id,$v['id']);
					}
				}
			}
		}
		return true;
	}

	/**
	 * 更新父ID
	 * @author workflow Develop Team
	 * @param  
	 * @return int/bool/object/array
	 */
	function updateProductParentClass($parentId,$new_id){
		$value_array = array();
		$value_array['pc_u_id'] = $new_id;
		$result = $GLOBALS['db']->UpdateRow($parentId,$value_array,'product_class','pc_u_id');
		return $result;
	}
	
	/**
	 * 查找某商品分类没有深度路径的最高父分类
	 *
	 * @param int $parentId
	 * @return bool
	 */
	function updateProductClassDeepPath($classID,$level = 0){
		$parent_array = $GLOBALS['db']->GetAllRow("product_class","pc_id,pc_u_id,pc_deeppath"," where pc_u_id='" . $classID . "'");
		if (count($parent_array) > 0){
			foreach($parent_array as $k=>$v){
				$value_array = array();
				$value_array['pc_deeppath'] = $level+1;
				$result = $GLOBALS['db']->UpdateRow($v['pc_id'],$value_array,'product_class','pc_id');
				$this->updateProductClassDeepPath($v['pc_id'],$level+1);
			}
		}
		return true;
	}
	
	/**
	 * 转移类别下的信息到另一个类别中
	 * @author workflow Develop Team     
	 * @param  $move_array需要转移的ID数组,$to_id转入的类别ID
	 * @return bool
	 */
	function moveProductClass($move_array,$to_id){
		if (intval($to_id) > 0 && !empty($move_array)){
			foreach ($move_array as $k => $v){
				$condition_str[] = "pc_id = '" . $v . "'";
			}
			$condition_str = implode(' or ',$condition_str);
			$value_array = array();
			$value_array['pc_u_id'] = intval($to_id);
			if (substr(trim($condition_str),0,3) == 'and'){
				$condition_str = substr(trim($condition_str),3);
			}
			$result = $GLOBALS['db']->UpdateRows('product_class',$value_array,$condition_str);
			unset($condition,$condition_str,$value_array);
			return $result;
		}
		return false;
	}
}
?>